<?php

/**
 * 带原型管理器的原型模式
 */

/**
 * 抽象原型
 */
interface Prototype {  
    public function copy();  
}  

/**
 * 具体原型
 */
class Color implements Prototype
{
	private $red;
	private $green;
	private $blue;
	
	public function __construct($red, $green, $blue)
	{
		$this->red = $red;
		$this->green = $green;
		$this->blue = $blue;
	}
	
	public function setRed($red)
	{
		$this->red = $red;
	}
	
	public function getRed()
	{
		return $this->red;
	}
	
	public function setGreen($green)
	{
		$this->green = $green;
	}
	
	public function getGreen()
	{
		return $this->green;
	}
	
	public function setBlue($blue)
	{
		$this->blue = $blue;
	}
	
	function display() {  
        echo $this->red , ',', $this->green, ',', $this->blue ,'<br>';  
    }  
	
	public function getBlue()
	{
		return $this->blue;
	}
	
	public function copy()
	{
		return clone $this;
	}
}

/**
 * 原型管理器
 */
class ColorManager
{
	static $colors = array();
	
	public static function add($name, $value)
	{
		self::$colors[$name] = $value;
	}
	
	public static function get($name)
	{
		return self::$colors[$name];
	}
}

class Client
{
	public static function main()
	{
		//原型：白色  
        ColorManager::add("white", new Color( 255, 0, 0 ));  
          
        //红色可以由原型白色对象得到，只是重新修改白色: r  
        $red = ColorManager::get('white');  
        $red->setRed(255);  
        $red->display();  
          
        //绿色可以由原型白色对象得到，只是重新修改白色: g  
        $green = ColorManager::get('white');  
        $green->setGreen(255);  
        $green->display();  
          
        //绿色可以由原型白色对象得到，只是重新修改白色: b  
        $Blue = ColorManager::get('white');  
        $Blue->setBlue(255);  
        $Blue->display();  
	}
}

Client::main();